package com.wh.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * 
 *  Class Name: BufferTest2.java
 *  Description: 进行文件流拷贝的第二种方法，直接进行管道的拷贝,这种也是common-util中使用的方法
 *  @author wanghao  DateTime 2017年1月19日 下午1:44:03 
 *  @company hongbo
 *  @email 1228675455@qq.com
 */
public class BufferTest2 {

	public static void main(String[] args) {
		FileInputStream fis = null;
		FileChannel inchannel = null;
		FileOutputStream fos = null;
		FileChannel outchannel = null;
		try {
			fis = new FileInputStream("C://Users//wh//Desktop//openid上传文件说明(1).txt");
			fos = new FileOutputStream("D://nio/copy2.txt");
			inchannel = fis.getChannel();
			outchannel = fos.getChannel();
			long size = inchannel.size();
			if(size > 0) {
				outchannel.transferFrom(inchannel, 0, size);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(inchannel != null) {
				try {
					inchannel.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(outchannel != null) {
				try {
					outchannel.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
